**********************************************************************
*Deal summary statistics 
**********************************************************************
*I. Generate deal table
*1. Input data 
	{
	*Targets
	use "$reg\FULLsampleis", clear 
	ren wt_cemm_s wt_cemmis
	append using "$reg\FULLsampless"
	ren wt_cemm_s wt_cemmss
	
	foreach var in incl_1is incl_1ss wt_cemmis wt_cemmss {
	replace `var' = 0 if missing(`var')
	}
	keep if incl_1is | incl_1ss 
	keep if (incl_1is & wt_cemmis > 0) | (incl_1ss & wt_cemmss > 0) 
	
	keep num ye *hhi* incl_*
	gduplicates report num ye 
	compress
	tempfile numTOT1
	save `numTOT1', replace
	
	keep num 
	gduplicates drop 
	tempfile TARGETS
	save `TARGETS', replace 

	*Lagged vars 
	*aha_merge6_1.dta contains hospital-year level data from the AHA survey and 
	*identified mergers, described in Appendix A1, A3. 
	use num ye bought bdtot admtot np fp using "$aha\aha_merge6_1", clear			
	local out bdtot admtot np fp 
	sort num ye
	foreach var in `out' {
	gen     prea_`var' = `var'[_n-1] if num==num[_n-1]
	replace prea_`var' = `var'       if missing(prea_`var')
	}
	keep num ye prea_*
	compress
	tempfile numTOT2
	save `numTOT2', replace
	}
	
*2. Prepare data 
	{
	*AHA data, excluding system creations
	*aha_merge6_1_exclcreate.dta contains hospital-year level data from the AHA 
	*survey and identified mergers, excluding instances where two hospitals join 
	*together to form a new system. Described in Appendix A1, A3. 
	use "$aha\aha_merge6_1_exclcreate.dta", clear
	keep if hosptype=="gac"
	drop if hosp_cntrl=="fed"
	drop if stcd < 10
	
	*Add system-to-system (SS) deals 
	replace sysid     = 908       if inlist(num,310003,310039) & ye==2015
	replace bought_ss = bought_is if inlist(num,310003,310039)
	replace bought_is = 0         if inlist(num,310003,310039)
	
	*Fill in locations 
	sort num 
	forval i = -5/5 {
	replace hrr = hrr[_n-`i'] if num==num[_n-`i'] & missing(hrr)
	}
	sort mloczip
	forval i = 1/10 {
	replace hrr = hrr[_n-`i'] if missing(hrr)
	replace hrr = hrr[_n+`i'] if missing(hrr)
	}	
	count if missing(hrr)
	assert r(N)==0 
	drop if never==1
	
	*Latitude & longitude
	*aha_vol_clean contains latitude and longitude variables from the AHA 
	merge 1:1 num ye using "$aha\vol\aha_vol_clean", keepusing(lat long_)
	ren lat lat2 
	ren long_ long_2
	bysort num: gegen lat = mean(lat2)
	bysort num: gegen long_ = mean(long_2)
	drop lat2 long_2
	drop if _m==2
	drop _m 
	
	*Flag single-GAC AHA systems 
	keep num ye sysid stcd hrrc bought bought_?s never bdtot np fp admtot ofint lat long_ 
	bysort sysid ye: gegen ct = count(ye)
	gen flag_one = ct==1 
	bysort num: gegen flag_onehospinsys = max(flag_one)
	drop flag_one ct 
	
	*Prior system vars 
	preserve
	drop if missing(sysid)
	bysort sysid ye: gegen prior_sys_bd  = sum(bdtot)  
	bysort sysid ye: gegen prior_sys_adm = sum(admtot) 
	bysort sysid ye: gegen prior_sys_ct  = nvals(num)  
	keep sysid ye prior_sys_*
	gduplicates drop 
	ren sysid prior_sysid
	replace ye = ye+1 
	tempfile priorsys
	save `priorsys', replace
	restore
	
	*Fill in prior sysid
	xtset num ye
	gen prior_sysid = L1.sysid
	forval i = 2/5 {
	replace prior_sysid = L`i'.sysid if missing(prior_sysid) & bought_ss==1
	}
	replace prior_sysid = 897  if num==670021  & ye==2015
	replace prior_sysid = 1775 if num==440144  & ye==2013
	count if missing(prior_sysid) & bought_ss==1 & ye!=2011 
	assert r(N)==0 
	
	*System first year
	bysort sysid: gegen fstyr = min(year)	
	replace fstyr = . if missing(sysid)	
	}
	
*3. Classify deals
	{
	*Limit to obs in deals and deal years  
	drop if ye < 2012 
	drop if missing(sysid)
	gegen deal = group(sysid ye)
	bysort deal: gegen hasbought = max(bought)
	replace deal = . if hasbought!=1
	drop hasbought
	drop if missing(deal)
	gdistinct deal
	sum deal, d
	assert r(max) < 4000
	
	*Classify deals as IS/SS 
	bysort deal: gegen has_ss = max(bought_ss)
	bysort deal: gegen has_is = max(bought_is)	
	gen deal_is = deal        if has_is
	gen deal_ss = deal + 4000 if has_ss
	order deal*
	
	*Define acquirers & targets 
	gen newsys_is = (ye==fstyr & bought_is==1)
	gen newsys_ss = (ye==fstyr & bought_ss==1)
	gen acq_is    = (!missing(deal_is)) & (bought!=1 | newsys_is==1)
	gen acq_ss    = (!missing(deal_ss)) & (bought!=1 | newsys_ss==1)
	gen tar_is    = (!missing(deal_is) & bought_is==1)
	gen tar_ss    = (!missing(deal_ss) & bought_ss==1)
	count if tar_is & tar_ss
	assert r(N)==0
	
	gen tar = tar_is | tar_ss
	gen acq = acq_is | acq_ss
	tab tar acq, m
	count if !tar & !acq
	assert r(N)==0 
	drop deal
	
	*IS dealids 
	*Assumes each IS target is bought separately
	gen deal_tar_isc   = deal_is if newsys_is
	gen deal_tar_ssc   = deal_ss if newsys_ss
	sum deal_tar_isc   
	assert r(max) < 2500 
	
	gegen deal_tar_is2   = group(deal_is num) if !newsys_is & tar_is
	replace deal_tar_is2 = deal_tar_is2 + 2500
	gen deal_tar_is      = deal_tar_isc
	replace deal_tar_is  = deal_tar_is2 if missing(deal_tar_is)
	drop deal_tar_is2
	sum deal_tar_is
	assert r(max) < 3000 
	count if tar_is & missing(deal_tar_is)
	assert r(N)==0 
	
	*SS dealids  
	*Assumes SS targets at the same prior system are bought together
	gegen deal_tar_ss   = group(deal_ss prior_sysid) if tar_ss
	replace deal_tar_ss = deal_tar_ss + 3000
	gen deal_tar = deal_tar_is
	replace deal_tar = deal_tar_ss if !missing(deal_tar_ss)
	count if tar_ss & missing(deal_tar_ss)
	assert r(N)==0 
	
	*Save file
	preserve
	keep num ye deal* tar* acq stcd
	sort num ye
	save "$aha\num_dealcodes", replace
	restore
	drop has_* never fstyr
	}
	
*4. Generate deal characteristics 
	*4.1 Targets in Elevance sample
	{		
	preserve
	*Limit to targets in Elevance 
	keep if tar		
	merge m:1 num_prvdr_num using `TARGETS', keepusing(num)
	count if _m==2
	assert r(N)==0 
	keep if _m==3
	drop _m
	
	*Merge in target characteristics 
	merge m:1 num ye using `numTOT2', keepusing(prea_*)
	drop if _m==2
	drop _m	
	local out bdtot admtot np fp 
	foreach var in `out' {
	ren prea_`var' pret_`var'
	}
	
	merge m:1 num_prvdr_num year using `numTOT1', keepusing(*hhi*)
	foreach var in hhi p_dhhi dhhi {
	ren `var' `var'2
	bysort num: gegen `var' = max(`var'2)
	drop `var'2
	}
	drop if _m==2
	drop _m	
	count if missing(hhi)
	assert r(N)==0 
	
	replace prior_sysid = . if tar_ss!=1
	merge m:1 prior_sysid ye using `priorsys'
	tab _m if        !missing(prior_sysid) 
	count if _m==1 & !missing(prior_sysid) 
	assert r(N)==0
	drop if _m==2
	drop _m
	
	tempfile targets
	save `targets', replace
	restore
	}
	
	*4.2 Acquirers in AHA sample 
	{
	preserve
	keep if acq
	drop deal_tar*
	
	*N hospitals, beds, admissions per acquirer
	merge 1:1 num ye using `numTOT2', keepusing(prea_*)
	drop if _m==2
	drop _m	
	
	foreach is in is ss {
	bysort deal_`is': gegen prea_ct_`is'      	 = count(num) 		 	 if !missing(deal_`is')
	foreach var in bdtot admtot {
	bysort deal_`is': gegen prea_t`var'_`is'     = sum(prea_`var')  	 if !missing(deal_`is')		
	}
	
	label var prea_ct_`is'      "Acq N hospitals"
	label var prea_tbdtot_`is'  "Acq tot beds"
	label var prea_tadmtot_`is' "Acq tot admits"
	}
	
	keep deal_*s num lat long_ hrrc prea*_?s
	local prea prea*
	unab prea: `prea'
	
	sort deal_is deal_ss num
	bysort deal_is deal_ss: gen ct = _n
	reshape wide num lat long_ hrrc, i(deal_is deal_ss) j(ct)
	tempfile acqs
	save `acqs', replace
	restore
	}
	
	*4.3 Targets & acquirers in same market
	{
	use `targets', clear
	drop prior_sysid acq* 
	order deal*
	
	merge m:1 deal_is deal_ss using `acqs'
	count if _m==1 
	assert r(N)==0
	drop if _m==2
	drop _m
	
	*Exclude target from acquirers 
	gegen ct = rownonmiss(num*)
	sum ct, d
	local m = r(max) - 1
	drop ct
	forval i = 1/`m' {
	local cond num_prvdr_num==num_prvdr_num`i'
	replace lat`i'=.   	  	  	 if `cond'
	replace long_`i'=. 	  	  	 if `cond'
	replace hrrcode`i'=.  	  	 if `cond'
	replace num_prvdr_num`i' = . if `cond'
	}
	
	*Same market
	ren num_prvdr_num num
	gen acqdist_min = .
	gen same_mkt = 0 
	gen same_hrr = 0 
	
	forval i = 1/`m' {		
	qui gen lata = lat`i'
	qui gen long_a = long_`i'
	qui geodist lat long_ lata long_a, gen(len) mi	
	
	replace acqdist_min  = min(acqdist_min,len)
	replace same_hrr 	 = same_hrr + 1 if hrrcode==hrrcode`i'
	replace same_mkt 	 = same_mkt + 1 if len<=15 & !missing(len)
	drop len lata long_a
	}
	
	foreach var in same_mkt same_hrr {
	replace `var' = . if missing(acqdist)
	replace `var' = 0 if missing(`var') & flag_one==1
	}
	drop acqdist_min
	drop num_prvdr_num* lat* long* hrrcode*
	ren num num_prvdr_num
	}
	
	*4.4 Targets in regression sample 
	{
	merge 1:1 num ye using `numTOT1', keepusing(incl*)
	drop if _m==2
	drop _m

	foreach is in is ss {
	replace deal_tar_`is' = . if !inrange(ye,2013,2017) 
	replace deal_tar_`is' = . if incl_1`is'==0  
	}
	drop if missing(deal_tar_is) & missing(deal_tar_ss)
	
	*Exclude 2nd deals
	sort num ye 
	replace deal_tar_ss = . if num==num[_n-1] & !missing(deal_tar_ss[_n-1])
	replace deal_tar_ss = . if num==num[_n-1] & !missing(deal_tar_is[_n-1]) & abs(ye-ye[_n-1]) < 4	
	drop if missing(deal_tar_is) & missing(deal_tar_ss)
	drop incl_1*s
	
	*Save 
	preserve
	keep num ye deal* tar* 
	sort num ye
	save "$aha\num_dealcodes_regsample1", replace
	restore
	}
	
*5 Summarize 
	*5.1 Collapse & summarize 
	{
	*Collapse to deal level 
	drop if missing(deal_tar_ss) & missing(deal_tar_is)
	merge m:1 num ye using "$aha\acq_dist", keepusing(acqdist_min)		
	drop if _m==2
	drop _m 
	
	local preis pre*is
	local press pre*ss
	unab preis: `preis'
	unab press: `press'
	foreach is in is ss {
	foreach var in `pre`is'' {
	replace `var' = . if missing(deal_tar_`is')
	}
	}
	
	foreach is in tar_is tar_ss {
	bysort deal_`is': gegen pre_ct_`is'      	= count(num) 			if !missing(deal_`is')
		
	foreach var in bdtot admtot {
	bysort deal_`is': gegen pre_`var'_`is'      = sum(pret_`var') 		if !missing(deal_`is')
	replace pre_`var'_`is' = . 			      if missing(pre_`var'_`is') & !missing(deal_`is')	
	}
	
	foreach var in np fp {
	bysort deal_`is': gegen pre_`var'_`is'      = mean(pret_`var') 		if !missing(deal_`is')
	replace pre_`var'_`is' = . 			      if missing(pre_`var'_`is') & !missing(deal_`is')	
	}
	
	foreach var in same_hrr {
	bysort deal_`is': gegen has_`var'_`is'		= max(`var')	 		if !missing(deal_`is')
	bysort deal_`is': gegen mean_`var'_`is'		= mean(`var')	 		if !missing(deal_`is')
	replace has_`var'_`is' = 1 if has_`var'_`is' > 0 & !missing(has_`var'_`is')
	}
	
	foreach var in hhi dhhi p_dhhi {
	bysort deal_`is': gegen mean_`var'_`is'		= mean(`var') 			if !missing(deal_`is')	
	}
	
	foreach var in acqdist_min {
	bysort deal_`is': gegen min_`var'_`is'		= min(`var') 			if !missing(deal_`is')	
	}

	foreach var in prior_sys_bd prior_sys_ct {
	bysort deal_`is': gegen mean_`var'_`is'		= mean(`var') 			if !missing(deal_`is')	
	}
	
	label var pre_ct_`is'            "Tar N hospitals"
	label var pre_bdtot_`is'         "Tar beds" 
	label var pre_admtot_`is'        "Tar admits"
	label var pre_np_`is'            "Tar NP"
	label var pre_fp_`is'            "Tar FP"
	label var has_same_hrr_`is'      "Tar same HRR as acq"
	label var mean_hhi_`is'          "Tar HHI"
	label var mean_dhhi_`is'         "Tar dHHI"
	label var mean_p_dhhi_`is'       "Tar predicted dHHI"
	label var min_acqdist_min_`is'   "Tar distance to acq"
	label var mean_prior_sys_bd_`is' "Tar prior system beds" 
	label var mean_prior_sys_ct_`is' "Tar prior system N hosps"
	}
	
	foreach var in incl* flag* pret* num bought* same* hhi hhi_hsa dhhi p_dhhi* acqdist* newsys* tar_?s tar prior_sys_bd prior_sys_ct bdtot admtot stcd np fp pos_pdhhi* {
	capture noisily drop `var'
	}

	gduplicates drop
	isid deal_tar	
	order deal*
	qui compress
	save "$aha\deal_table1", replace	
	
	*Summarize
	*Table 2
	keep deal_tar_?s has_same_hrr_tar_* mean_dhhi_tar_* *prior* mean_*hhi_tar_* min_acqdist* pre_admtot_tar* pre_bdtot_tar* pre_ct_tar* pre_?p_tar* prea_ct* prea_tadmtot* prea_tbdtot*
	foreach var in deal_tar_is deal_tar_ss {
	gegen N_`var' = count(`var')
	drop `var'
	}	
	ds N_deal_tar_is N_deal_tar_ss, not 
	foreach var in `r(varlist)' {
	gegen MEAN_`var' = mean(`var')
	gegen MED_`var'  = median(`var')
	drop `var'
	}
	gduplicates drop 
	qui compress
	save "$op\deal_table_outputs", replace	
	fsum 
	pause 
	
	*Acquired hospitals per year
	*Figure 2(a)
	use "$aha\num_dealcodes_regsample1", clear	
	keep ye tar deal_tar_?s
	keep if tar==1
	tab ye if !missing(deal_tar_is)
	tab ye if !missing(deal_tar_ss)
	pause 
	}

	*5.2 Save 
	foreach is in is ss {
	use "$aha\deal_table1", clear
	keep deal_tar_`is'* pre_ct_tar_`is' prea_ct_`is' 
	drop if missing(deal_tar_`is')
	gduplicates drop
	count 

	merge 1:m deal_tar_`is' using "$aha\num_dealcodes_regsample1", keepusing(num ye) 
	keep if _m==3
	drop _m 	
	compress
	save "$reg\reg_dealcodes`is'match1", replace
	}
